<template>

  <!-- 加解密配置 -->
  <div class="encrypt-decrypt">
    <el-dialog
      title="加解密配置"
      :visible.sync="dialogVisible"
      width="800px"
    >
      <el-form ref="form" :model="form" :rules="formRules" label-width="130px">
        <el-form-item label="加解密名称:" prop="encryptDecryptName">
          <el-input v-model="form.encryptDecryptName" clearable :maxlength="50" placeholder="请输入加解密名称" />
        </el-form-item>
        <el-form-item label="加解密方法:" prop="method">
          <el-select v-model="form.method" clearable placeholder="请选择加解密方法">
            <el-option
              v-for="(item, index) in dictOptions.encryptMethod"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.method && Number(form.method) <= 6" label="密钥:" prop="commonKey">
          <el-input v-model="form.commonKey" type="textarea" :rows="3" clearable :maxlength="500" placeholder="请输入密钥" />
        </el-form-item>
        <el-form-item v-if="form.method && Number(form.method) > 6" label="公钥:" prop="publicKey">
          <el-input v-model="form.publicKey" type="textarea" :rows="3" clearable :maxlength="500" placeholder="请输入公钥" />
        </el-form-item>
        <el-form-item v-if="form.method && Number(form.method) > 6" label="私钥:" prop="privateKey">
          <el-input v-model="form.privateKey" type="textarea" :rows="3" clearable :maxlength="500" placeholder="请输入私钥" />
        </el-form-item>
        <el-form-item label="备注:" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" :rows="3" clearable :maxlength="100" placeholder="请输入备注" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button v-if="form.id" type="danger" @click="delBtn">删 除</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="!canClick" :loading="!canClick" @click="encryptDecryptConfirm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { EAndDAddApi, EAndDDetailApi, EAndDDeleteApi } from '@/api/serviceManage/requestPartyServiceManage'
import dictMixin from '@/utils/mixins/dict'
export default {

  mixins: [
    dictMixin(['encryptMethod'])
  ],

  data () {
    return {
      canClick: true,
      dialogVisible: false,
      form: {
        serviceId: '',
        encryptDecryptName: '',
        method: '',
        publicKey: '',
        privateKey: '',
        remarks: ''
      },
      formRules: {
        encryptDecryptName: { required: true, message: '请输入加解密名称', trigger: 'blur' },
        method: { required: true, message: '请选择加解密方法', trigger: 'change' }
      }
    }
  },

  methods: {

    // 确定按钮
    encryptDecryptConfirm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.canClick = false
          const params = this.form
          if (Number(this.form.method) > 6) {
            delete params.commonKey
          } else {
            delete params.publicKey
            delete params.privateKey
          }
          EAndDAddApi({
            ...params
          }, this.form.id)
            .then(({ code, data }) => {
              if (code === '00000') {
                this.$message.success('配置成功')
                this.dialogVisible = false
                this.$emit('encryptDecryptRefresh')
              }
            }).finally(() => {
              this.canClick = true
            })
        }
      })
    },

    // 删除按钮
    delBtn () {
      this.$confirm('确定删除加解密配置？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        EAndDDeleteApi({
          ids: [this.form.id]
        }).then(({ code, data }) => {
          if (code === '00000') {
            this.$message.success('删除成功')
            this.dialogVisible = false
            this.$emit('encryptDecryptRefresh')
          }
        })
      })
    },

    // 获取各种初始数据
    async getInitData (row) {
      this.dialogVisible = true
      this.form = {
        serviceId: row.id,
        encryptDecryptName: '',
        method: '',
        commonKey: '',
        publicKey: '',
        privateKey: '',
        remarks: ''
      }
      await this.$nextTick()
      this.$refs.form.resetFields()
      if (row.encryptDecryptId) {
        EAndDDetailApi({
          id: row.encryptDecryptId
        }).then(({ code, data }) => {
          if (code === '00000' && data) {
            this.form = {
              serviceId: row.id,
              encryptDecryptName: data.encryptDecryptName,
              id: row.encryptDecryptId,
              method: data.method,
              commonKey: data.commonKey,
              publicKey: data.publicKey,
              privateKey: data.privateKey,
              remarks: data.remarks
            }
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.encrypt-decrypt {
  ::v-deep .el-dialog__body {
    padding: 0 20px;
    height: 450px;
    overflow-y: auto;
  }
}

</style>
